package la.renzhen.rtpt.spring.boot;

import com.ecwid.consul.transport.TLSConfig;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 云动态参数配置<p>
 *
 * @author <a href="mailto:zhouhaichao@2008.sina.com">haiker</a>
 * @version 2018/05/05
 */
@Data
@ToString
@ConfigurationProperties(RTPTProperties.PREFIX)
public class RTPTProperties {

    public static final String PREFIX = "rtpt";

    /**
     * 统一前缀，减少内容过度加载的风险
     */
    String prefix = "";

    /**
     * 环境, 源上无效
     */
    String environment = "master";

    /**
     * 重新加载策略,如果等于0，只在启动是加载一次,默认1分钟加载一次,
     */
    long refreshStrategy = TimeUnit.MINUTES.toSeconds(1);

    @Data
    public static class Redis {
        public static final String PREFIX = RTPTProperties.PREFIX + ".redis";

        /**
         * 统一存储前缀
         */
        String folder = "rtpt";

        private String address;

        /**
         * Database index used by the connection factory.
         */
        private int database = 0;

        /**
         * Redis server host.
         */
        private String host = "localhost";

        /**
         * Login password of the redis server.
         */
        private String password;

        /**
         * Redis server port.
         */
        private int port = 6379;

        /**
         * Enable SSL.
         */
        private boolean ssl;

        /**
         * Connection timeout in milliseconds.
         */
        private int timeout = (int) TimeUnit.SECONDS.toMillis(1);

        private Pool pool = new Pool();

        /**
         * Pool config.
         */
        @Data
        public static class Pool {

            /**
             * Max number of "idle" connections in the pool. Use a negative value to indicate
             * an unlimited number of idle connections.
             */
            private int maxIdle = 8;

            /**
             * Target for the minimum number of idle connections to maintain in the pool. This
             * setting only has an effect if it is positive.
             */
            private int minIdle = 0;

            /**
             * Max number of connections that can be allocated by the pool at a given time.
             * Use a negative value for no limit.
             */
            private int maxActive = 8;

            /**
             * Maximum amount of time (in milliseconds) a connection allocation should block
             * before throwing an exception when the pool is exhausted. Use a negative value
             * to block indefinitely.
             */
            private int maxWait = -1;
        }
    }

    Redis redis;

    @Data
    public static class Consul {
        public static final String PREFIX = RTPTProperties.PREFIX + ".consul";

        /**
         * consul配置地址
         */
        String address = "localhost:8500";

        /**
         * 统一存储前缀
         */
        String folder = "rtpt";

        TLSConfig tlsConfig;
    }

    Consul consul;

    @Data
    public static class Center {
        public static final String PREFIX = RTPTProperties.PREFIX + ".center";

        @Data
        public static class Discovery {
            /**
             * 启用分布式方式
             */
            boolean enabled = false;
            /**
             * 分布式服务ServiceID
             */
            String serviceId = "rptp-config-server";
        }

        String address = "http://localhost:7658";
        String username;
        String password;
        String authorization;

        /**
         * 远程中心访问contextPath
         */
        String contextPath = "/rtpt";

        Map<String, String> headers = new HashMap<>();

        Discovery discovery = new Discovery();
    }

    Center center;
}
